#pragma once

#include <bondlib/RealTimeDeal/IRealTimeDealModel.h>
#include "SortBuffer.h"

namespace detail{
	class CBondUtil{

	public:
		static double GetBuyPrice(const BrokerKey& key);
		static double GetSellPrice(const BrokerKey& key);
		static double GetModifyTime(const BrokerKey& key);
		static REPORT* GetCompanyInfoById(const std::string& szKey, const BrokerKey& szID);
	};
	class DealRecordReader;
	class RealTimeDealSorter{
		enum kOrderChange{				// 列排序规则是否变化
			kOrderChangeSame,			// 列规则无变化,无须重新排序
			kOrderChangeReverse,		// 列规则只有一个列排序,且是反向,只需要将原先结果反序即可
			kOrderChangeReorder,		// 列规则需要重新排序
		};
	public:
		typedef DealIndex	KeyType;
	protected:
		std::vector<SortInfo>	m_sortinfos;		// 多重排序,大多数情况下只有一个
		int					m_record_size;		// 一条记录的字节


		int					m_record_head_size;	// KeyType的字节大小
		CSortBuffer*		m_sort_buffer;		// 持有的一个指针


		// 用于根据KeyType,读取每个要排序字段的工具
		DealRecordReader*	m_bond_reader;

		// 比较两条记录时的临时缓存
		char*				m_buffer;			// 全排后的所有排序记录的内存
		int					m_capcity;			// m_buffer 的实际内存大小
	public:
		RealTimeDealSorter();
		~RealTimeDealSorter();
		int GetRecordSize()const{ return m_record_size; }

		//==============================================================================
		// Summary:
		//     -compare two BrokerKey
		// Parameters:
		//     -k1		: first BrokerKey
		//     -k2		: last BrokerKey
		// Return:
		//		-1	means k1<k2
		//		0	means k1==k2
		//		1	means k1>k2
		// Remarks:
		//     -
		// Usage:
		//     -compare two BrokerKey and decide where to insert so as to keep container ordered
		//     -used in JumpList::Comparator
		//==============================================================================
		int CompareIndex(const KeyType& b1, const KeyType& b2);
		void BindCompareMap(const IRecordMap* records, bool bind);


		//==============================================================================
		// Summary:
		//     -sort BrokerKey
		// Parameters:
		//     -orders	: come from CSortInfoManager,multi column sort info
		//     -keys	: array of BrokerKey to be sorted
		//     -ncorpid	: need when sort (kTknCleanPrice|kTknYield|kCleanPriceDiv|kYieldDiv|VTKNPRICE)
		//     -force	: always sort
		// Return:
		//     -
		// Remarks:
		//     -when sort bond records,CBondSorter build each short record by orders
		//     -each short record is made up of one BrokerKeyInfo struct and several fixed bytes
		//     -of field of CBondInfo.These field data is accessed by BondReader::GetRecordField
		//     -Each field's size of memory is set by SetField.The short record's size can be calculated
		//     -by: sizeof(BrokerKeyInfo)+SUM(m_fields[column].field_bytes)
		// Usage:
		//     -rebuild orders of large array of BrokerKey
		//==============================================================================
		//void Sort(const vector<ColumnOrder>& orders, vector<BrokerKeyInfo>& keys, int ncorpid, bool force = false);
		void Sort(const std::vector<ColumnOrder>& orders, std::vector<KeyType>& keys, const IRecordMap* records, bool force);

		kOrderChange SetSortInfo(const std::vector<ColumnOrder>& orders);
	};
}
